﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ross.EntityFramework;
using Ross.EntityFramework.Model;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using System.Linq.Expressions;

namespace Ross.Service
{
    public class FAQsService : BaseService, IDisposable
    {
        private TranDbContext dbContext;
        public IRepository<FAQs, int> repository;
        public FAQsService(string nameOrConnectionString)
        {
            dbContext = new TranDbContext(nameOrConnectionString);
            repository = new Repository<FAQs, int>(dbContext);
        }
        public void Dispose()
        {
            dbContext.Dispose();
        }
        //添加
        public ResultDto<bool> Create(Dto.FAQs faq)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                var obj = repository.Insert(Mapper.Map<FAQs>(faq));
                result.status = 200;
                result.datas = true;
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.datas = false;
            }
            return result;
        }
        //删除
        public ResultDto<bool> Delete(int id)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                repository.Delete(id);
                result.status = 200;
                result.datas = true;
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.status = 500;
                result.datas = false;
            }
            return result;
        }
        //编辑
        public ResultDto<bool> Update(Dto.FAQs faq)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                var model = repository.Get(faq.ID);
                model.AskTitle = faq.AskTitle;
                model.AskContent = faq.AskContent;
                repository.InsertOrUpdate(Mapper.Map<FAQs>(model));
                result.status = 200;
                result.datas = true;

            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.status = 500;
                result.datas = false;
            }
            return result;
        }
        //回答
        public ResultDto<bool> Answer(int id, string answerContent)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                var model = repository.Get(id);
                model.AnswerContent = answerContent;
                model.IsClosed = true;
                repository.InsertOrUpdate(Mapper.Map<FAQs>(model));
                result.status = 200;
                result.datas = true;

            }
            catch (Exception ex)
            {
                result.message = ex.ToString();
                result.status = 500;
                result.datas = false;
            }
            return result;
        }
        //获取单页
        public ResultDto<Dto.FAQs> GetByID(int id)
        {
            ResultDto<Dto.FAQs> result = new ResultDto<Dto.FAQs>();
            var obj = repository.Get(id);
            if (obj != null)
            {
                result.status = 200;
                result.datas = Mapper.Map<Dto.FAQs>(obj);
            }
            else
            {
                result.message = "id is not exist";
                result.status = 500;
            }
            return result;
        }
        //获取列表
        public ResultDto<List<Dto.FAQs>> GetPageList(int pageindex = 1, int pagesize = 10, long userId = 0, long teacherId = 0, string keywords = "", int isClosed = -1)
        {
            ResultDto<List<Dto.FAQs>> result = new ResultDto<List<Dto.FAQs>>();
            Expression<Func<FAQs, bool>> expression = o => o.CourseID > 0;
            if (isClosed >= 0)
                expression = expression.ExpressionAnd(t => t.IsClosed == isClosed > 0);
            if (!string.IsNullOrEmpty(keywords))
                expression = expression.ExpressionAnd(t => t.AskTitle.Contains(keywords));
            if (userId > 0)
                expression = expression.ExpressionAnd(t => t.UserID == userId);
            if (teacherId > 0)
                expression = expression.ExpressionAnd(t => t.TeacherID == teacherId);
            var resp = repository.GetPageList(pageindex, pagesize, expression);
            result = Mapper.Map<ResultDto<List<Dto.FAQs>>>(resp);
            return result;
        }

    }
}
